package com.example.spider;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ReUtil;
import com.example.spider.config.RestTemplateConfiguration;
import com.example.spider.provider.why10w.dto.WhyTaskResult;
import org.shoulder.core.util.JsonUtils;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;

public class Thn21PageTester {

    private static RestTemplate rest = RestTemplateConfiguration.getInstance(Charset.forName("GBK"));

    public static void main(String[] args) {
        testNormalPage();
    }

    private static void testNormalPage(){
        String url = "https://www.thn21.com/Article/wai/5551_2.html";
        String result = rest.getForObject(url, String.class);
        String content = ReUtil.findAllGroup1("<p align=\"center\"(.*?)<p align=\"center\"", result).get(0);
        String regex = "<a href=\"(.*?)\" target=\"_blank\" title=\".*?\">(.*?)</a>";
        String category = ReUtil.findAll("<strong>(.*?)</strong></p>", content, 1).get(0);
        List<String> detailUrlList = ReUtil.findAll(regex, content, 1);
        List<String> titleList = ReUtil.findAll(regex, content, 2);
        titleList = titleList.stream()
                // 去掉 ◆
                .map(s -> s.replace("◆", ""))
                // 需要转义 &quot;
                .map(s -> s.replace("&quot;", "\""))
                .map(String::trim)
                .collect(Collectors.toList());
        System.out.println();
    }

    private static void testEndPage(){
        String url = "https://www.thn21.com/Article/wai/5551_7.html";
        String result = rest.getForObject(url, String.class);
        String content = ReUtil.findAllGroup1("<p align=\"center\"(.*?)<p align=\"center\"", result).get(0);
        //     List<String> charsetList = ReUtil.findAllGroup1("<meta charset=(.*)[\"|>]", result);
        String regex = "<a href=\"(.*?)\" target=\"_blank\" title=\".*?\">(.*?)</a>";
        //List<String> all = ReUtil.findAll(regex, content, 0);
        List<String> detailUrlList = ReUtil.findAll(regex, content, 1);
        List<String> titleList = ReUtil.findAll(regex, content, 2);
        // 去掉 ◆ trim
        // 需要转义 &quot;
        // 有的详情页带#
        System.out.println();
    }

    private static void testDetail(){
        String url = "https://www.thn21.com/Article/wai/zhishi/shiwan/123.html";
        String html =  rest.getForObject(url, String.class);
        String category = "信息科技";
        String title = "为什么计算机会说话？";
        String contentStart = "<p>" + category + "·" + title + "</p>";
        int start = html.indexOf(contentStart);
        int end = html.indexOf("<p>上一篇：");
        if(start < 0 || start > end){
            // 结束了

        }
        String content = html.substring(start + contentStart.length(), end);

//        String imageUrlRex = "src=\"(.*?)\" class=\"attachment-gridlove-single size-gridlove-single wp-post-image";
//        String imageUrl = ReUtil.findAllGroup1(imageUrlRex, content).get(0);

        String id = url.substring(url.lastIndexOf('/', url.lastIndexOf('.')));
        System.out.println(content);
    }
    private static void testDetail2(){
        String url = "https://www.thn21.com/Article/wai/zhishi/shiwan/kexue_1.html";
        String txt = FileUtil.readString("F:\\spider\\www.thn21.com\\10why.txt", StandardCharsets.UTF_8);
        List<String> titleList = ReUtil.findAllGroup1("<strong><a name=.+?>(.+?)</a></strong>", txt);
        List<String> contentList = ReUtil.findAllGroup1("</strong>(.+?)<p><strong>", txt);
        Map<String, WhyTaskResult> whyTaskResultMap = new HashMap<>(500);
        for (int i = 0; i < titleList.size(); i++) {
            String id = UUID.randomUUID().toString();
            WhyTaskResult whyTaskResult = WhyTaskResult.builder()
                    .id(id)
                    .title(titleList.get(i))
                    .url(url)
                    .content(contentList.get(i))
                    .categoryList(List.of("科学知识"))
                    .tagList(Collections.emptyList())
                    .imageInfoList(Collections.emptyList())
                    .build();
            whyTaskResultMap.put(url + "#" + i, whyTaskResult);
        }

        // 保存到文件
        String fileName = "F:/spider/www.thn21.com/result.dict";
        File file = FileUtil.touch(fileName);
        FileUtil.writeString(JsonUtils.toJson(whyTaskResultMap), file, StandardCharsets.UTF_8);
    }

}
